layui.use(['form', 'table','layuimini','element'], function () {
    var $ = layui.jquery,
        form = layui.form,
        table = layui.table,
        layuimini = layui.layuimini;

    /**
     * 初始化表单，要加上，不然刷新部分组件可能会不加载
     */
    form.render();

    var BoInfoSet = {
        id: 'BoInfoSetTable'
    };

    var column = [
        [
            {type: 'numbers'},
            {field: 'title', title: '空间标题', align: 'center'},
            {field: 'honeyTalk', title: '蜜语', align: 'center'},
            {field: 'musicName', title: '音乐名称', minWidth: 120, align: 'center'},
            {field: 'isOpen', title: '是否开放', minWidth: 120, align: 'center',
                templet: function (object) {
                    if(object.isOpen == 0){
                        return '<input type="checkbox" lay-filter="l_isOpenSwitch" lay-skin="switch" lay-text="是|否" value="' + object.id + '">';
                    }else{
                        return '<input type="checkbox" lay-filter="l_isOpenSwitch" lay-skin="switch" lay-text="是|否" value="' + object.id + '" checked>';
                    }
                }
            },
            {templet: '#stateBtn', width: 140, align: 'center', title: '操作'}
        ]
    ];

    table.render({
        elem: '#' + BoInfoSet.id,
        url: '/web/boInfoSet/list',
        toolbar: '#toolbarBtn',
        defaultToolbar: ['filter', 'exports', 'print'],
        cols: column,
        limits: [10, 15, 20, 25, 50, 100],
        limit: 15,
        page: true
    });

    /**
     * 点击编辑按钮
     * @param id
     */
    BoInfoSet.edit = function(id){
        var content = layuimini.getHrefContent('/web/boInfoSet/addOrEdit?id=' + id);
        var index = layer.open({
            title: '编辑配置',
            type: 1,
            shade: 0.2,
            maxmin:true,
            shadeClose: false,
            zIndex: 998,
            area: ['600px','500px'], //宽高
            content: content,
            end: function(){
                //刷新表格
                table.reload(BoInfoSet.id);
            }
        });

        return false;
    };

    //监听是否开放开关
    form.on('switch(l_isOpenSwitch)', function(data){
        var isOpen = 0;
        if(data.elem.checked){
            isOpen = 1;
        }

        var infoSetId = data.value; //编号

        //更新状态
        var indexLoad = layer.load(2);
        var url = '/web/boInfoSet/updateIsOpen';
        $.post(url, {isOpen: isOpen, infoSetId: infoSetId}, function(data){
            layer.close(indexLoad);
            if(data.state == "200"){
                layer.msg(data.message, {icon: 1});
            }else{
                layer.msg(data.message, {icon: 2});
                //更新失败则改状态
                if(data.elem.checked){
                    data.elem.checked = false;
                }else{
                    data.elem.checked = true;
                }
                form.render();
            }
        })
    });

    //监听表格复选框选择
    // table.on('checkbox(currentTableFilter)', function (obj) {
    //     console.log(obj)
    // });

    //监听工具条
    table.on('tool(' + BoInfoSet.id + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            BoInfoSet.edit(data.id)
        }
    });

});